<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use \app\data\Data;
use app\api\model\News as news_list;

class Banner extends Index
{
    public function banner_index()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 10;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();
        $status = (isset($_GET['status']) && $_GET['status']) ? $_GET['status'] : array('neq', 3);
        $map['b.title'] = array('like', "%$search%");
        $map['b.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $map['b.status'] = $status;
        $map['b.sheet'] = trim(input('get.sheet'));
        $alias = 'b';
        $news_list = new news_list('banner');
        $count = $news_list->count($map, $alias);
        $data = $news_list->banner_list($map, $number, $page);
        //dump($count);dump($data);
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', 'banner列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', 'banner列表数据获取失败', null);
        }
    }

    public function banner_add()
    {
        $files = request()->file('img');
        $news_list = new news_list('banner');
        $img = $news_list->upload($files, 'Images/Banner');
        if (!$img) {
            action_log('Banner新增', 'log_admin');
            $this->json('1000', 'banner添加失败', null);
        }
        $arr['img_url'] = $img;
        $res = $news_list->banner_add($arr);
        if ($res) {
            action_log('Banner新增', 'log_admin',1);
            $this->json('200', 'banner添加成功', null);
        } else {
            action_log('Banner新增', 'log_admin');
            $this->json('1000', 'banner添加失败', null);
        }
    }

    public function banner_detail()
    {
        $id = input('get.id');
        $news_list = new news_list('banner');
        $res = $news_list->news_detail($id);
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', 'banner查询成功', $res);
        } else {
            $this->_jsonp('1000', 'banner查询失败', null);
        }
    }

    public function banner_edit()
    {
        $news_list = new news_list('banner');
        $arr = array();
        if (!empty($_FILES)) {
            $files = request()->file('img');
            $img = $news_list->upload($files, 'Images/Banner');
            $arr['img_url'] = $img;
        }
        $res = $news_list->banner_edit($arr);
        if (isset($res) && !empty($res)) {
            action_log('Banner编辑', 'log_admin',1);
            $this->json('200', 'banner修改成功', $res);
        } else {
            action_log('Banner编辑', 'log_admin');
            $this->json('1000', 'banner修改失败', null);
        }
    }

    public function banner_del()
    {
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $news_list = new news_list('banner');
            $data = $news_list->news_del($id, $data);
            if ($data['res']) {
                action_log('Banner修改状态', 'log_admin',1);
                $this->_jsonp('200', $data['name'] . '成功', null);
            } else {
                action_log('Banner修改状态', 'log_admin');
                $this->_jsonp('1000', $data['name'] . '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /*政务工作资讯平台首页*/
    public function sheet_index()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 10;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();
        $status = (isset($_GET['status']) && $_GET['status']) ? $_GET['status'] : array('neq', 3);
        $types = (isset($_GET['type']) && $_GET['type']) ? $_GET['type'] : array('neq', 0);
        $sheet = (isset($_GET['sheet']) && $_GET['sheet']) ? $_GET['sheet'] : '';
        if (empty($sheet)) {
            $this->_jsonp('1000', 'banner列表数据获取失败', null);
        }//sheet为1代表政务工作banner，为2代表平台资讯banner
        switch ($sheet) {
            case 1:
                $type = 'zgh_government_type';
                break;
            default:
                $type = 'zgh_platform_type';
                break;
        }
        $map['w.type'] = $types;
        $map['w.title'] = array('like', "%$search%");
        $map['w.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $map['w.status'] = $status;
        $map['w.sheet'] = $sheet;
        $alias = 'w';
        $news_list = new news_list('banner_sheet');
        $count = $news_list->count($map, $alias);
        $condition = 'w.type=t.id';
        $data = $news_list->news_list($type, $map, $number, $page, $condition);
        //dump($count);dump($data);
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', 'banner列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', 'banner列表数据获取失败', null);
        }

        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();


        $where['sheet'] = 2;
        $where['status'] = (isset($_GET['status']) && !empty($_GET['status'])) ? $_GET['static'] : array('neq', 0);
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['title'] = array('like', "%$username%");


        $zgh_material = new Data('banner');
        $res['data'] = $zgh_material->selectPage($page, $where);
        $res['count'] = $zgh_material->count($where);
        if (isset($res) || empty($res)) {
            $this->_jsonp('200', '获取人才培训信息成功', $res);
        } else {
            $this->_jsonp('1000', '获取人才培训信息失败', null);
        }

    }

    public function sheet_add()
    {
        $files = request()->file('img');
        $news_list = new news_list('banner_sheet');
        $img = $news_list->upload($files, 'Images/BannerSheet');
        if (!$img) {
            action_log('Banner政务新增', 'log_admin');
            $this->json('1000', 'banner添加失败', null);
        }
        $arr['img_url'] = $img;
        $arr['sheet'] = input('post.sheet');
        $arr['type'] = input('post.type');
        $res = $news_list->banner_add($arr);
        if ($res) {
            action_log('Banner政务新增', 'log_admin',1);
            $this->json('200', 'banner添加成功', null);
        } else {
            action_log('Banner政务新增', 'log_admin');
            $this->json('1000', 'banner添加失败', null);
        }
    }

    public function sheet_detail()
    {
        $id = input('get.id');
        $news_list = new news_list('banner_sheet');
        $res = $news_list->news_detail($id);
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', 'banner查询成功', $res);
        } else {
            $this->_jsonp('1000', 'banner查询失败', null);
        }
    }

    public function sheet_edit()
    {
        $news_list = new news_list('banner_sheet');
        $arr = array();
        if (!empty($_FILES)) {
            $files = request()->file('img');
            $img = $news_list->upload($files, 'Images/BannerSheet');
            $arr['img_url'] = $img;
        }
        $arr['type'] = input('post.type');
        $res = $news_list->banner_edit($arr);
        if (isset($res) && !empty($res)) {

            action_log('Banner政务编辑', 'log_admin',1);
            $this->json('200', 'banner修改成功', $res);
        } else {
            action_log('Banner政务编辑', 'log_admin');
            $this->json('1000', 'banner修改失败', null);
        }
    }

    public function sheet_del()
    {
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $news_list = new news_list('banner_sheet');
            $data = $news_list->news_del($id, $data);
            if ($data['res']) {
                action_log('Banner政务修改状态', 'log_admin',1);
                $this->_jsonp('200', $data['name'] . '成功', null);
            } else {
                action_log('Banner政务修改状态', 'log_admin');
                $this->_jsonp('1000', $data['name'] . '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}

    }
}